from urlparse import urljoin

def remWhiteSpace(link):
	newLink = ''
	for j in range(len(link)):
		if link[j] == '"' or link[j] == ' ' or link[j] == '\n':
			continue
		else:
			newLink = newLink + link[j]
	return newLink

def MyParser(fname) :
	fp = open(fname,'r')
	doc = fp.read()

	data = {}
	listLink = []
	listRes = []
	docno = ''
	docurl = ''
	flag = 1

	for i in range(len(doc)):

		if doc[i:i+7] == '<docno>' or doc[i:i+7] == '<DOCNO>':
			n = doc[i+7:].find('<')
			docno = doc[i+7:i+n+7]
			docno = remWhiteSpace(docno)
			i = i + 7 + n + 8
#			if flag == 1:
#				print docno
		
		if doc[i:i+8] == '<dochdr>' or doc[i:i+8] == '<DOCHDR>':
			n = doc[i+8:].find(' ')
			docurl = doc[i+8:i+n+8]
			l = docurl.rfind(':')
			if l != -1:
				f = docurl[l:].find('/')
				if f != -1:
					docurl = docurl[:l] + docurl[l+f:]
				else :
					docurl = docurl[:l]
			docurl = remWhiteSpace(docurl)
			i = i + 8 + n + 9
			if flag == 1:
#				print docurl
				flag = 0
		
		if doc[i:i+7] == '<a href' or doc[i:i+7] == '<A HREF':
			l = doc[i+7:].find('=')
			f = doc[i+7+l:].find('>')
			tempLink = doc[i+8+l:i+7+l+f]
			link = remWhiteSpace(tempLink)
			link = urljoin(docurl,link)
			listLink.append(link)
			i = i + 7 + l + f
#			print "Indexing link " + link

		if doc[i:i+6] == '</doc>' or doc[i:i+6] == '</DOC>':
			listRes.append(docurl)
			listRes.append(listLink)
			data[docno] = listRes
			flag = 1
			
			listLink = []
			listRes = []
			docno = ''
			docurl = ''
			i = i + 6
	fp.close()
	return data
